package com.lyp.crm.util;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSession;
import com.lyp.crm.service.Logjdao;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lyp.crm.entity.Logj;
import com.lyp.crm.entity.Student;
import com.lyp.crm.entity.User;
@Component
public class LogAspect {
	@Autowired
	private Logjdao logjdao;
	/**
  * ǰ��֪ͨ
  * @param jp
  */
	public void before(JoinPoint jp){ 
		String methodName = jp.getSignature().getName(); 
		Object[] args = jp.getArgs();//Ŀ�귽�����в��� 
		
	}
	/**
	 * ����֪ͨ
	 * @param jp
	 * @throws Throwable
	 */
	public void afterReturning(JoinPoint jp) throws Throwable {
		//��֪ͨ���ȡsession��һ�ַ�������
		ServletRequestAttributes attr = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
    	
		//��ȡsession
		HttpSession session=attr.getRequest().getSession(true);
		//��ȡsession�е�Ա����¼��Ϣ
    	User user = (User)session.getAttribute("list");
    	//Ŀ�귽�����в��� 
        Object[] args = jp.getArgs(); 
        //����һ����־����
        Logj logj =new Logj();
        //��ȡִ�еķ�������
		String methodName = jp.getSignature().getName();
		
		Integer lstid=null;//ѧ��id
		String lstname=null;//ѧ������
		Integer lusid=null;//Ա��id
		String lusname=null;//Ա������
		Integer mid=null;//ģ��id
		Integer rid=null;//��ɫid
		String loperate=null;//����
		String lreserved2=null;//��ע
		if("insertModules".equals(methodName)){
			loperate="����";
			lreserved2="������ģ����Ϣ";
		}
		if("deleteModules".equals(methodName)){
			loperate="ɾ��";
			lreserved2="ɾ����"+mid+"ģ����Ϣ";
		}
		if("updateModules".equals(methodName)){
			loperate="�޸�";
			lreserved2="�޸���"+mid+"ģ����Ϣ";
		}
		if("deleteRoles".equals(methodName)){
			loperate="ɾ��";
			lreserved2="ɾ����"+rid+"��ɫ��Ϣ";
		}
		if("updateRoles".equals(methodName)){
			loperate="�޸� ";
			lreserved2="�޸���"+rid+"��ɫ��Ϣ";
		}
		//ѧ������
		if("insertStudent".equals(methodName)){
			loperate="����";
			//��ȡ���ӵ�ѧ����Ϣ
			Student student=(Student) args[0];
			//����ѧ��id
			lstid=50;
			//��ȡѧ��name
			lstname=student.getSname();
			//ƴ����־����
			lreserved2=user.getUname()+"������"+lstname+"��ѧ����Ϣ";
			//��ȡ�û�id
			lusid=user.getUid();
			//��ȡ�û���
			lusname=user.getUname();
			//ִ��������־����
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));
			
		}
		if("updateStudent".equals(methodName)){
			Student student=(Student) args[0];
			lstname=student.getSname();
			lstid=student.getSid();
			lusid=user.getUid();
			lusname=user.getUname();
			loperate="�޸�";
			lreserved2=user.getUname()+"�޸���"+lstname+"ѧ����Ϣ";
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));

		}
		if("delectStudent".equals(methodName)){
			lstid=(Integer) args[0];
			lusid=user.getUid();
			lusname=user.getUname();
			loperate="ɾ��";
			lreserved2=user.getUname()+"ɾ���˱��"+lstid+"��ѧ����Ϣ";
		
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));

	
		}
		if("login".equals(methodName)){
			loperate="��¼";
			lreserved2="��¼��";
			User user1=(User) args[0];
			lusid=user1.getUid();
			if (user1.getUname()!=null) {
				lusname=user1.getUname();
				logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));
			} 
		}
		if("indeteUser".equals(methodName)){
			User user1=(User) args[0];
			String lusname1=user1.getUname();
			loperate="����";
			lreserved2=user.getUname()+"������"+lusname1+"��Ա����Ϣ";
			lusid=user.getUid();
			lusname=user.getUname();
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));

		}
		if("deleteUser".equals(methodName)){
			lusid=(Integer) args[0];
			lusname=user.getUname();
			loperate="ɾ��";
			lreserved2=user.getUname()+"ɾ���˱��Ϊ"+lusid+"��Ա����Ϣ";
			lusid=user.getUid();
			lusname=user.getUname();
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));

		}
		if("updateUser".equals(methodName)){
			User user1=(User) args[0];
			String lusname1=user1.getUname();
			lstid=user1.getUid();
			loperate="�޸�";
			lreserved2=user.getUname()+"�޸���"+lusname1+"��Ա����Ϣ";
			lusid=user.getUid();
			lusname=user.getUname();
			logjdao.insertLogj(new Logj(currentTime(),lstid,lstname,lusid,lusname,loperate,lreserved2));

		}

		
		
       /* System.out.println(methodName);
        for (int i = 0; i < args.length; i++) {
			System.out.println(args[i]);
		}*/
	}
	/**
	 * ��ȡʱ��
	 * @return
	 */
	public String currentTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
}
